<?php
class cronControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
		session_write_close();
		set_time_limit(300);
 
	}
	
	public function onDefault(){
		$data=M("cron")->selectRow(array("order"=>"next_time asc","where"=>" next_time<".time()));
		if(!empty($data)){
			$data['last_time']=time();
			$data['next_time']=time()+$data['minute']*60;
			M("cron")->update($data,"id=".$data['id']);
			if(strpos($data['url'],"http")===false){
				$fun="on".trim($data['url']);
				self::$fun();
			}else
				file_get_contents($data['url']);
			} 
	}
	
	public function onDo(){
		 
		$key="index_cron_do";
		if(!cache()->get($key)){
			cache()->set($key,1,1);
			$this->onKouDaiDz();
			$this->onYmdian_dakuan();
			$this->onCache();
			C("cron_img")->onDefault();
			
			C("push")->onPlan();
			C("push")->onPlan(1);
			C("push")->onPlan(2);
			C("push")->onSendSms();
			
			
		}
	}
	/*清除过期缓存*/
	public function onCache(){
		$key="index_cron_cache";
		if(!cache()->get($key)){
			cache()->set($key,1,1);
			M("dbcache")->delete(" expire<".time());
		}
	}
	
	/**
	**口袋对账 任务
	**/	 
	public function onKouDaiDz(){
		$time=3600*24*2;
		$list=M("koudai_order")->select(array(
			"where"=>" dateline>".$time." AND status=3 AND isdz=0 AND tx_dz_admin=0 ",
			"limit"=>100
			
		));
		M("koudai_order")->begin();
		if($list){
			foreach($list as $v){
				/*推送信息*/
				$adminids=M("koudai_admin")->selectCols(array(
					"where"=>" shopid=".$v['shopid']." AND typeid=2 ",
					"fields"=>" adminid "
				));
				if($adminids){
					$content=date("H:i:s")."您的有订单".$v['orderno']."需要对账了，赶紧去处理下吧";
					//发送通知
					foreach($adminids as $adminid){
						M("koudai_notice")->insert(array(
									"dateline"=>time(),
									"content"=>date("H:i:s")."您的有订单".$v['orderno']."需要对账了，赶紧去处理下吧",
									"adminid"=>$adminid
						));
					}
					//发送推送
					$pushlist=M("apppush")->select(array(
						"where"=>"  koudaiadmin in("._implode($adminids).") "
					));
					if($pushlist){					 
						
						foreach($pushlist as $v){
							$pdata=array(
								"pid"=>$v['id'],
								"typeid"=>1,
								"dateline"=>time(),
								"content"=>date("H:i:s")."您的有订单".$v['orderno']."需要对账了，赶紧去处理下吧",
							);
							M("apppush_plan")->insert($pdata);								
						}
					}
					
				}
				
				M("koudai_order")->update(array(
					"tx_dz_admin"=>1,
				),"order_id=".$v['order_id']);
				
				
			}
		}
		M("koudai_order")->commit();
		
	}
	
	/**夜猫店 打款**/
	
	public function onYmdian_dakuan(){
		//只在5点前执行
		//if(date("H")>5) return false;
		$key="cron_ymdian_dakuan";
		if(!$c=cache()->get($key)){
			cache()->set($key,30);
		}else{
			echo "dakuan error";
			 
		}
		$now=strtotime(date("Y-m-d"));
		$stime=time()-3600*24*2;
		$endtime=time()+3600*24*3;
		//查找5天内的未结账
		for($i=0;$i<20;$i++){
			$kdays[]=date("Ymd",strtotime("-$i days",$stime));
		}
		$content=date("Y-m-d")."结算打款";
		$yms=M("ymdian")->select(array("where"=>"1=1"));
		 
		foreach($yms as $shop){
			M("ymdian")->begin();
			$row=M("ymdian_dakuan")->selectRow(array("where"=>" shopid=".$shop['id'],"order"=>"id DESC"));
			
			if(empty($row)){
				//三天前的订单
				$daysorder=M("stat_shoporder_day")->select(array(
					"where"=>" k='ymdian' and kday in("._implode($kdays).") AND jiesuan=0 AND shopid=".$shop['id']." "
				));
			}else{
				if($row['dateline']>$stime){
					continue;
				}
				$daysorder=M("stat_shoporder_day")->select(array(
					"where"=>" k='ymdian' and kday in("._implode($kdays).") AND jiesuan=0 AND shopid=".$shop['id']." "
				));
			}
			  
			if($daysorder){
				$money=0;
				foreach($daysorder as $v){
					$ids[]=$v['id'];
					$money+=$v['off_money'];
				}
				M("stat_shoporder_day")->update(array(
					"jiesuan"=>1
				),"id in("._implode($ids).") ");
				//加入打款
				M("ymdian_dakuan")->insert(array(
					"siteid"=>$shop['siteid'],
					"shopid"=>$shop['id'],
					"money"=>$money,
					"dateline"=>time(),
					"endtime"=>$endtime,
					"content"=>$content
				));
			}
			
			M("ymdian")->commit();
		}
		echo "success";
	}
	
	/*cron_img*/ 

	
	
	
}

?>